package artid.com.arttogo.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;

import java.util.ArrayList;
import java.util.List;

import artid.com.arttogo.R;
import artid.com.arttogo.adapter.AuctionPastAuction_Adapter;
import artid.com.arttogo.bean.AuctionAuctionListParser;
import artid.com.arttogo.db.Cache;
import artid.com.arttogo.network.VolleyBasic;
import artid.com.arttogo.network.VolleyCallback;
import artid.com.arttogo.network.VolleyData;
import artid.com.arttogo.network.VolleyMap;
import artid.com.arttogo.utils.NetUtils;
import artid.com.arttogo.view.pulllist.PullToRefreshLayout;
import artid.com.arttogo.view.pulllist.PullableListView;

/**
 * 
 *    
 * 项目名称：ArtworkArt   
 * 类名称：AuctionAll_Activity   
 * 类描述：所有拍卖会   
 * 创建人：耿卫斌   
 * 创建时间：2017-3-29 上午9:34:06   
 *
 */
public class AuctionAll_Activity extends BaseActivity implements OnClickListener {
	private ImageView searchBtn;
	private PullableListView mListView;
	private PullToRefreshLayout mPullLayout;
	private LinearLayout noDataLL;
	private boolean isRefreshed = false;// 是否刷新
	private int page = 1;
	private AuctionPastAuction_Adapter mAdapter;
	private List<AuctionAuctionListParser> list=new ArrayList<AuctionAuctionListParser>();
	private List<AuctionAuctionListParser> more_list=new ArrayList<AuctionAuctionListParser>();
	private String organizationID;//机构ID
	private String imgwide;// 请求的图片宽度
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_auctionall);
		initView();
	}

	private void initView(){
		mContext=AuctionAll_Activity.this;
		imgwide = (Integer.parseInt(NetUtils.getDisplayMetrics((Activity) mContext).get(NetUtils.DM_WIDTH)) / 1) + "";
		organizationID=getIntent().getStringExtra("organizationID");
		mBack = (ImageView) findViewById(R.id.back_btn);
		mBack.setOnClickListener(this);
		mTittle = (TextView) findViewById(R.id.tittle_text);
		mTittle.setText("所有拍卖");
		searchBtn=(ImageView) findViewById(R.id.AuctionAll_title_searchBtn);
		searchBtn.setOnClickListener(this);
		mListView = (PullableListView) findViewById(R.id.AuctionAll_ListView);
		mListView.setOnItemClickListener(onItemClick);
		noDataLL = (LinearLayout) findViewById(R.id.ll_hint);
		mPullLayout = (PullToRefreshLayout) findViewById(R.id.AuctionAll_refresh_view);
		mPullLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {

			@Override
			public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
				// TODO Auto-generated method stub
				// 下拉刷新
				page = 1;
				isRefreshed = true;
				getData();
			}

			@Override
			public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
				// TODO Auto-generated method stub
				// 上拉加载
				page++;
				getData();
			}
		});
		getData();
	}


	@Override
	public void onClick(View v) {
		Intent intent;
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back_btn:
			finish();
			break;
		case R.id.AuctionAll_title_searchBtn:
			//搜索
			intent=new Intent(mContext,LotSearchActivity.class);
			startActivity(intent);
			break;

		default:
			break;
		}
	}

	/**
	 * item点击事件
	 */
	OnItemClickListener onItemClick=new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
								long arg3) {
			// TODO Auto-generated method stub
			Intent i=new Intent(mContext,AuctionInfo_Activity.class);
			i.putExtra("auctionID", more_list.get(position).getId());
			i.putExtra("SpecialListData", 0);
			startActivity(i);
		}
	};

	/**
	 * 获取拍卖会数据
	 * 机构id	id	当类型是1时，id必传值
		页码	page	
		条数	pagesize	
		类型	type	默认是1，2表示往期拍卖会
	 */
	private void getData(){
		VolleyBasic.getInstance(this).post(Cache.auction_auctionList,
				VolleyMap.getInstance().auction_auctionList(page, 10, organizationID, "1",imgwide),
				AuctionAuctionListParser.class, new VolleyCallback() {

			@SuppressWarnings({ "unchecked", "static-access" })
			@Override
			public void onSuccess(VolleyData vd) {
				// TODO Auto-generated method stub
				mPullLayout.loadmoreFinish(mPullLayout.SUCCEED);
				list=(List<AuctionAuctionListParser>) vd.getList();
				if (isRefreshed == true) {
					more_list.clear();
					isRefreshed = false;
				}
				if (!list.isEmpty()) {
					more_list.addAll(list);
				}

				//这里是显示有数据和无数据的布局
				if(more_list!=null && more_list.size()!=0){
					noDataLL.setVisibility(View.GONE);
				}else{
					noDataLL.setVisibility(View.VISIBLE);
					return;
				}

				if (mAdapter == null) {
					mAdapter=new AuctionPastAuction_Adapter(mContext, more_list);
					mListView.setAdapter(mAdapter);
				}
				mAdapter.notifyDataSetChanged();
			}

			@Override
			public void onError(VolleyError volleyError) {
				// TODO Auto-generated method stub
				mPullLayout.loadmoreFinish(mPullLayout.FAIL);
				//这里是显示有数据和无数据的布局
				if(more_list!=null && more_list.size()!=0){
					noDataLL.setVisibility(View.GONE);
				}else{
					noDataLL.setVisibility(View.VISIBLE);
					return;
				}
			}
		});
	}
}	
